package main

import (
	"fmt"
	"math/rand"
	"time"
)

//func main() {
//	go exerciseOne()
//	go exerciseTwo()
//	select {}
//}

/**
练习题一
*/
func exerciseOne() {
	list := [5]string{"I", "am", "stupid", "and", "weak"}
	for i := range list {
		if i == 2 {
			list[i] = "smart"
		}
		if i == 4 {
			list[i] = "strong"
		}
	}
	fmt.Println("Array item:", list)
}

func exerciseTwo() {
	fmt.Println("main starting")
	channel := make(chan int, 10)
	go producer(channel)
	go consumer(channel)
	select {}
	fmt.Println("main exit")
}

func producer(channel chan<- int) {
	timer := time.NewTicker(time.Second)
	for range timer.C {
		nano := rand.New(rand.NewSource(time.Now().UnixNano()))
		random := nano.Int()
		channel <- random
		fmt.Printf("producer channel send data %d \n", random)
	}
}

func consumer(channel <-chan int) {
	timer := time.NewTicker(time.Second)
	for range timer.C {
		select {
		case <-channel:
			fmt.Printf("consumer channel get data %d \n", <-channel)
		}
	}
}
